<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Front extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	
	function __construct() {
		parent::__construct();
		$this->init();
		$this->load->helper('file');
		$this->load->model(array('contents_model','board_model','gallery_model','common_model','setting_model'));
		$this->load->library('image_lib');
		
	}
	
	/**
	 * main html page
	 *
	 * @access	public
	 * @return	array
	 */
	function main(){
		$skin = 'main';
		$data = array(
				'_title' => '메인',
				'_content' => null,
				'_skin' => $skin,
				'_front' => 'Y'
		);
		return $data ;
	}
	
	/**
	 * front html page
	 *
	 * @access	public
	 * @return	array
	 */
	function html(){
		$url = $this->common_model->get_index_uri(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$id = urldecode($this->security->xss_clean(url_explode($uri_array, 'id')));
		//$skin = in_array('skin', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'skin'))) : 'main';
		$skin = 'sub';
		$data = array(
				'_title' => '사이트 디자인 미리보기',
				'_content' => './content/'.$id.'.html',
				'_skin' => $skin,
				'_front' => 'Y',
				'_menu_id' => $id
		);
		$content = $this->contents_model->select($id);
		$content = array('_title'=>$content['title'],'_group'=>$content['parent_id']);
		$data = array_merge($data,$content );
		return $data ;
	}
	
	/**
	 * 메뉴 미설정 페이지
	 *
	 * @access	public
	 * @return	array
	 */
	function waiting(){
		$url = $this->common_model->get_index_uri(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$id = urldecode($this->security->xss_clean(url_explode($uri_array, 'id')));
		//$skin = in_array('skin', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'skin'))) : 'main';
		$skin = 'sub';
		
		$data = array(
				'_title' => '작업 진행중입니다.',
				'_content' => 'waiting',
				'_skin' => $skin,
				'_front' => 'Y',
				'_group' => null,
				'_menu_id' => null
		);
		return $data ;
	}
	
	/*
	 @게시판 관리
	*/
	public function board(){
	
		$uri_array = segment_explode($this->uri->uri_string());
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$id = in_array('id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'id'))) : null;
		$no = in_array('no', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'no'))) : null;
		$action = in_array('board', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'board'))) : null;
	
		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		$url = $this->common_model->get_index_uri(3);
		$board_info = $this->board_model->board_info($id);
		$this->load->library('CHboard',array('config'=>$board_info,'table_name'=>'ch_board_'.$id,'skin'=>$board_info['board_skin']));
		$view = $this->chboard->getOutter();
		$board_name = 'ch_board_'.$id;
		$theme = $this->setting_model->getDesignDefault();
		switch($action){
			case('list'):
				$param['sch_keyname'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyname')));
				$param['sch_keyword'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyword')));
				$sch_url_pattern = search_param_url($param);
	
				$param = array_merge($param,array('page'=>$page,'id'=>$id,'board_name'=>$board_name));
	
				$listnum = $board_info['list_cnt'];
				$count = $this->board_model->list_total($param);
				$this->chboard->dress('board_list',$this->chboard->boardlist($param),$view);
				//@paging
				/*$this->load->library('pagination');
				
				
				$config['per_page'] = $listnum;
				$config['use_page_numbers'] = true;
				$config['uri_segment'] = $uri_segment;
				$this->pagination->initialize($config);
				*/
				$config['total_rows'] = $count;
				$config['base_url'] = '/front/board/list/id/'.$id.'/'.$sch_url_pattern.'/page';
				$config['page'] = $page;
				$config['listnum'] = $listnum;
				$config['block'] = 10;
				
				$config['theme'] = $theme['theme']; 
			
				//$this->chboard->dress('PAGING',$this->pagination->create_links(),$view);
				$this->chboard->dress('PAGING',$this->common_model->pagination($config),$view);
				$this->chboard->dress('skin_name',$board_info['board_skin'],$view);
				$this->chboard->dress('id',$id,$view);
				$this->chboard->dress('brd_btn_write','/front/board/write/id/'.$id,$view);
				$view = $this->chboard->removeTagSelected('no_data','',$view);
				break;
			case('write'):
				$view = $this->chboard->boardWrite(array('theme'=>$theme['theme'],'page'=>$page,'id'=>$id,'no'=>$no,'board_name'=>$board_name));
				break;
			case('reply'):
				$view = $this->chboard->boardReply(array('theme'=>$theme['theme'],'page'=>$page,'id'=>$id,'no'=>$no,'board_name'=>$board_name));
				break;
			case('view'):
				$view = $this->chboard->boardView(array('theme'=>$theme['theme'],'page'=>$page,'id'=>$id,'no'=>$no,'board_name'=>$board_name));
				break;
		}
		$view = $this->chboard->removeAllTags($view);

		$data = array(
				'_title' => '게시판 관리',
				'_group' => 'homepage',
				'_content' => $url,
				'_menu_id' => $id,
				'id'=>$id,
				'view'=>$view,
				'_skin' => 'sub',
				'_front' => 'Y',
		);
		$content = $this->contents_model->select($id);
		$content = array('_title'=>$content['title'],'_group'=>$content['parent_id']);
		$data = array_merge($data,$content );
		return $data;
	}
	
	function boardDbjob(){
		$id = $this->input->post('id',true);
		$title = $this->input->post('title',true);
		$writer = $this->input->post('writer',true);
		$content = $this->input->post('content',true);
		$dbjob = ($this->input->post('dbjob')) ? $this->input->post('dbjob',true) : 'i';
	
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'content' => $content ,
						'auth_uid' => 'admin',
						'depth' => 0,
						'ansnum'=> 0,
						'view_count'=> 0,
						'register_date'=>  date("Y-m-d H:i",time())
				);
				break;
			case('r'):
				$msg = '저장되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'content' => $content ,
						'auth_uid' => 'admin',
						'view_count'=> 0,
						'register_date'=>  date("Y-m-d H:i",time())
				);
				break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'content' => $content
	
				);
				break;
			case('d'):
				$msg = '삭제 되었습니다.';
				$data = null;
				break;
		}
	
		if(isset($_FILES['files'])){
			/*file upload begin*/
			$upload_path = convert_path('/upload/board/'.$id);
			$config['upload_path'] = $upload_path;
			$config['allowed_types'] = 'jpg|gif|png|zip|rar|doc|hwp|pdf|ppt|xls|pptx|docx|xlsx';
			$config['max_size']	= '10240'; //10MB
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$config['encrypt_name'] = true;
			$config['remove_spaces'] = true;
			$this->load->library('upload', $config);
			$this->upload->mkdir($upload_path);
		}
	
		if($this->board_model->board_dbjob($data)){
			if(isset($_FILES['files'])){
				$file = $this->upload->do_multi_upload('files');
				if($this->upload->display_errors()){
					$msg = $this->upload->display_errors('','');
				}else{
					$this->board_model->board_file($file);
				}
			}
			$this->alert->jalert($msg, base_url('/front/board/list/id/'.$id));
		}else{
			$this->alert->jalert('에러', '','back');
		}
		exit;
	}

	function boardCommentDbjob(){
		$board_id = $this->input->post('board_id',true);
		$board_no = $this->input->post('board_no',true);
		$b_comment = $this->input->post('b_comment',true);
		$user_id = $this->input->post('user_id',true);
		$name = $this->input->post('name',true);
		$content = $this->input->post('content',true);
		$dbjob = ($this->input->post('dbjob')) ? $this->input->post('dbjob',true) : 'i';
	
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'board_no' => $board_no ,
						'user_id' => $user_id ,
						'name' => $name ,
						'b_comment' => $b_comment,
						'register_ip'=> $this->input->ip_address(),
						'register_date'=>  date("Y-m-d H:i",time())
				);
				break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'b_comment' => $b_comment
				);
				break;
			case('d'):
				$data = null;
				$msg = '삭제 되었습니다.';
				break;
		}
	
		$this->board_model->board_comment_dbjob($data);
		$this->alert->jalert($msg, $this->input->server('HTTP_REFERER'));
	}
	
	
	
	/*
	 @갤러리 관리
	*/
	public function gallery(){
		$uri_array = segment_explode($this->uri->uri_string());
		$array_segment = array_keys($this->uri->segment_array(), "page");
		$page = in_array('page', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'page'))) : 1;
		$id = in_array('id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'id'))) : null;
		$no = in_array('no', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'no'))) : null;
		$action = in_array('gallery', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'gallery'))) : null;
	
		$uri_segment = in_array('page', $uri_array) ? $array_segment[0]+1 : 0;
		$url = $this->common_model->get_index_uri(3);
		$gallery_info = $this->gallery_model->gallery_info($id);
		$this->load->library('chgallery',array('config'=>$gallery_info,'table_name'=>'ch_gallery_'.$id,'skin'=>$gallery_info['gallery_skin']));
		
		$view = $this->chgallery->getOutter();
		$gallery_name = 'ch_gallery_'.$id;
		$theme = $this->setting_model->getDesignDefault();
		switch($action){
			case('list'):
				$param['sch_keyname'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyname')));
				$param['sch_keyword'] = urldecode($this->security->xss_clean(url_explode($uri_array, 'sch_keyword')));
				$sch_url_pattern = search_param_url($param);
	
				$param = array_merge($param,array('page'=>$page,'id'=>$id,'gallery_name'=>$gallery_name));
	
				$listnum = $gallery_info['cnt_row'];

				$count = $this->gallery_model->list_total($param);
				$this->chgallery->dress('gallery_list',$this->chgallery->gallerylist($param),$view);
				//@paging
				
				$config['total_rows'] = $count;
				$config['base_url'] = '/front/board/list/id/'.$id.'/'.$sch_url_pattern.'/page';
				$config['page'] = $page;
				$config['listnum'] = $listnum;
				$config['block'] = 10;
	
				$config['theme'] = $theme['theme'];
					
				//$this->chgallery->dress('PAGING',$this->pagination->create_links(),$view);
				$this->chgallery->dress('PAGING',$this->common_model->pagination($config),$view);
				$this->chgallery->dress('skin_name',$gallery_info['gallery_skin'],$view);
				$this->chgallery->dress('theme',$theme['theme'],$view);
				$this->chgallery->dress('id',$id,$view);
				$this->chgallery->dress('gal_btn_write','/front/gallery/write/id/'.$id,$view);
				$view = $this->chgallery->removeTagSelected('no_data','',$view);
				break;
			case('write'):
				$view = $this->chgallery->galleryWrite(array('theme'=>$theme['theme'],'page'=>$page,'id'=>$id,'no'=>$no,'gallery_name'=>$gallery_name));
				break;
			case('reply'):
				$view = $this->chgallery->galleryReply(array('theme'=>$theme['theme'],'page'=>$page,'id'=>$id,'no'=>$no,'gallery_name'=>$gallery_name));
				break;
			case('view'):
				$view = $this->chgallery->galleryView(array('theme'=>$theme['theme'],'page'=>$page,'id'=>$id,'no'=>$no,'gallery_name'=>$gallery_name));
				break;
		}
		$view = $this->chgallery->removeAllTags($view);
	
		
		
		$data = array(
				'_title' => '게시판 관리',
				'_group' => 'homepage',
				'_content' => $url,
				'_menu_id' => $id,
				'id'=>$id,
				'view'=>$view,
				'_skin' => 'sub',
				'_front' => 'Y',
		);
		$content = $this->contents_model->select($id);
		$content = array('_title'=>$content['title'],'_group'=>$content['parent_id']);
		$data = array_merge($data,$content );
		return $data;
	}
	
	
	function galleryDbjob(){
		
		$id = $this->input->post('id',true);
		$title = $this->input->post('title',true);
		$writer = $this->input->post('writer',true);
		$dbjob = ($this->input->post('dbjob')) ? $this->input->post('dbjob',true) : 'i';
	
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'auth_uid' => 'admin',
						'view_count'=> 0,
						'register_date'=>  date("Y-m-d H:i",time())
				);
				break;
			case('r'):
				$msg = '저장되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'auth_uid' => 'admin',
						'view_count'=> 0,
						'register_date'=>  date("Y-m-d H:i",time())
				);
				break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'title' => $title ,
						'writer' => $writer ,
						'content' => $content
	
				);
				break;
			case('d'):
				$msg = '삭제 되었습니다.';
				$data = null;
				break;
		}
		 
		if(isset($_FILES['files'])){
			/*file upload begin*/
			$upload_path = convert_path('/upload/gallery/'.$id);
			$config['upload_path'] = $upload_path;
			$config['allowed_types'] = 'jpg|gif|png';
			$config['max_size']	= '10240'; //10MB
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$config['encrypt_name'] = true;
			$config['remove_spaces'] = true;
			$this->load->library('upload', $config);
			$this->upload->mkdir($upload_path);
			$file = $this->upload->do_multi_upload('files');
			$file_info = '{"file_name":"'.$file[0]['file_name'].'","file_orinal":"'.$file[0]['orig_name'].'"}';
			$data = array_merge($data,array('file_info' => $file_info));
		}
	
		if($this->gallery_model->gallery_dbjob($data)){
			if(isset($_FILES['files'])){
				
				if($this->upload->display_errors()){
					$msg = $this->upload->display_errors('','');
				}else{
					unset($config);
					$gallery_info = $this->gallery_model->gallery_info($id);
			
					$config['source_image'] = '/upload/gallery/'.$id.'/'.$file[0]['file_name'];	
					$config['dynamic_output'] = TRUE;
					$config['create_thumb'] = TRUE;
					$config['maintain_ratio'] = TRUE;
					$config['width'] = $gallery_info['thumbnail_width'];
					$config['height'] = $gallery_info['thumbnail_height'];
					//$this->load->library('image_lib', $config);
					$this->image_lib->initialize($config);
					$this->image_lib->resize();
							
					echo $this->image_lib->display_errors();
					exit;
				}
			}
			$this->alert->jalert($msg, base_url('/front/gallery/list/id/'.$id));
		}else{
			$this->alert->jalert('에러', '','back');
		}
		exit;
	}
	
}

/* End of file html.php */
/* Location: ./application/controllers/html.php */